// pages/addWbId/addwbId.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [{
      id: '',
      val: ''
    }],
  },
  addList: function() {
    var lists = this.data.lists;
    var newData = {
      id: '',
      val: ''
    };
    lists.push(newData); //实质是添加lists数组内容，使for循环多一次
    this.setData({
      lists: lists,
    })
  },
  delList: function() {
    var lists = this.data.lists;
    lists.pop(); //实质是删除lists数组内容，使for循环少一次
    this.setData({
      lists: lists,
    })
  },
  bindInput: function(e) {
    let index = e.currentTarget.dataset.id;
    let list = this.data.lists
    list[index].val = e.detail.value;
    list[index].id = index;
    this.setData({
      lists: list
    })
  },
  postBtn:function () {
    let weiboids = []
    for (var i in this.data.lists) {
      weiboids.push(this.data.lists[i].val)
    }
    if ((new Set(weiboids)).size != weiboids.length) {
      wx.showToast({
        title: '请勿提交重复微博ID',
        icon: 'none'
      })
    } else {
      var  weiboidsStr = weiboids.join(',')
      wx.request({
        url: app.globalData.apiBaseURL + '/user/saveWeboIds',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          openId: app.globalData.databaseUser.userAppId,
          weiboids: weiboidsStr
        },
        success: function (data) {
          if(data.data.status == 1) {
            app.globalData.weiboIds = data.data.weiboIds
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                wx.switchTab({
                  url: '../register/register',
                })
              }
            })
           
          }
        }
      })
    }
    
  },
  getTips: function () {
    wx.navigateTo({
      url: '../guide/guide',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(app.globalData.userState)
    console.log(app.globalData.databaseUser)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})